﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SG.Logic;
using SG.Domain;
using SG.UI.Models;

namespace SG.UI.Controllers
{
    public class PoliticaController : Controller
    {
        private ExpArticulo exp = new ExpArticulo();
        //
        // GET: /Politica/

        public ActionResult Index()
        {
            return View(exp.GetAllPolitica());
        }



        //
        // GET: /Politica/Create

        public ActionResult CreateEdit(int? id)
        {
            Politica entity = id.HasValue ? exp.GetPolitica(id.Value) : new Politica();

            return View(entity);
        }


        [HttpPost]
        public ActionResult CreateEdit(int? id, FormCollection collection)
        {
            Politica entity = new Politica();
            float fAux;
            int iAux;
            DateTime daux;
            string err = "";
            try
            {
                entity.Id = id.HasValue ? id.Value : 0;

                entity.Nombre = collection["Nombre"];

                if (!String.IsNullOrEmpty(err)) throw new Exception(err);

                exp.SaveOrUpdatePolitica(entity);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                TempData["VMAlert"] = new VMAlert() { Title = "Error", Body = ex.Message };
                return View(entity);
            }
        }
       

      
        [HttpPost]
        public ActionResult Delete(FormCollection collection)
        {
            try
            {
                foreach (string item in collection["IdEntity"].Split(','))
                {
                    exp.DeletePolitica(int.Parse(item));
                }

            }
            catch (Exception ex)
            {
                TempData["VMAlert"] = new VMAlert() { Title = "Err", Body = ex.Message };
            }
            return RedirectToAction("Index");
        }

    }
}
